<template>
  <div class='container'>
    <el-dialog :title="title" :visible="dialogFormVisible" @close="closeDialog">
      <el-form :model="form" :rules="rules">
        <el-form-item label="新增文章" label-width="80px">
          <el-input placeholder="请输入文章标题" v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" label-width="80px">
          <quill-editor v-model="form.articleBody"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" label-width="80px">
          <el-input placeholder="请输入视频地址" v-model="form.videoURL" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'
export default {
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        videoURL: null,
        articleBody: null,
        title: null
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      },
      title: ''
    }
  },
  methods: {
    closeDialog () {
      this.$emit('update:dialogFormVisible', false)
    },
    async add () {
      if (this.title === '新增文章') {
        const res = await add(this.form)
        console.log(res)
        this.$message.success('添加成功')
      } else {
        const res = await update(this.form)
        console.log(res)
        this.$message.success('修改成功')
      }
      this.closeDialog()
      this.$emit('reload')
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .ql-editor {
  height: 250px;
}
</style>
